﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using cde.SystemCommon;
using Cit.CDE.Net.Module;
using Cit.CDE.Net.Module.Commons;
using Cit.CDE.Net.Service;
using Cit.CDE.Net.Service.ModuleFactory;
using Module;
using System.Text.RegularExpressions;

/// <summary>
/// Copyright (C) jinggubang
/// FileName:        UserEdit.aspx.cs       
/// Author :         wangbc
/// CDT:             2013-01-31
/// Version:         1.0.0.0                
/// Depiction:       用户管理新增&修改
/// </summary>
public partial class main_admin_UserEdit : UserControl.CommonPage
{

    /// <summary>
    /// 用户管理EM
    /// </summary>
    private CDEEntityManager emUserManage;
    /// <summary>
    /// 
    /// </summary>
    private CDESearchCriteria scUserManage;
    /// <summary>
    /// 用户管理List
    /// </summary>
    private List<CDEEntity> listUserManage;
    /// <summary>
    /// 用户管理Entity
    /// </summary>
    private CDEEntity m_entityUserManage;

    private CDEEntityManager emManage;

    public string chkID = "";

    private const string EM_FULL_NAME = Module.WEBConstants.EM_USER_MANAGE;

    /// <summary>
    /// Page_Load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        chkID = (string)Request.QueryString.Get("chkID");
        emUserManage = CDEEMFactory.getEMByFullName(EM_FULL_NAME);

        if (!Page.IsPostBack)
        {
            // ID不为空修改操作员，否则新增操作员
            if (chkID != null)
            {
                scUserManage = new CDESearchCriteria(emUserManage);
                
                // UserManage查询条件
                scUserManage.addCondition("ID", chkID);
                scUserManage.PageSize = 0;
                listUserManage = CDEService.LoadEntities(emUserManage, scUserManage);

                // 加载数据到页面
                if (listUserManage.Count > 0)
                {
                    m_entityUserManage = listUserManage[0];
                    m_entityUserManage.Operation = CDEDataOperation.UPDATE;
                    legend.InnerText = "修改用户";                 
                    row_PSW.Controls.Clear();
               
                }
                else
                {
                    m_entityUserManage = CDEEMServiceFactory.getEMService(emUserManage).CreateEntity();
                    m_entityUserManage.Operation = CDEDataOperation.INSERT;
                }
            } // end 修改操作员
            else
            {
                m_entityUserManage = CDEEMServiceFactory.getEMService(emUserManage).CreateEntity();
                m_entityUserManage.Operation = CDEDataOperation.INSERT;
            } // end 新增操作员


            // 置Session
            Session["MENTITY"] = m_entityUserManage;
            this.setControls(m_entityUserManage, Resources.ResourceErrMsg.ResourceManager);

        } // end 首次加载
        else
        {
            m_entityUserManage = (CDEEntity)Session["MENTITY"];
            if (chkID != null)
            {
                legend.InnerText = "修改用户";              
                row_PSW.Controls.Clear();
            }
            this.setEntityFromRequest(m_entityUserManage, Request.Params);
            //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", ((StringBuilder)Session["arrayJs"]).ToString());
        } // end 页面回发

    }
    /// <summary>
    /// 保存按钮事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {

        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
        if (m_entityUserManage.Operation == CDEDataOperation.INSERT)
        {
            m_entityUserManage.set("INSERT_USER", op.SysID);
            m_entityUserManage.set("INSERT_DATE", Now);
        }
        else
        {
            m_entityUserManage.set("UPDATE_USER", op.SysID);
            m_entityUserManage.set("UPDATE_DATE", Now);
        }

        // 更新用户信息
        listUserManage = new List<CDEEntity>();
        listUserManage.Add(m_entityUserManage);

        //检查用户名是否重复
        CDESearchCriteria scCheck = new CDESearchCriteria(emUserManage);
        if (chkID != null)
        {
            scCheck.addCondition("ID", chkID, CDEConstCompare.Inequality);
        }
        // 不能设DEL_FLG，因为USER_NAME是主键
        //scCheck.addCondition("DEL_FLG", "0");
        scCheck.addCondition("USER_NAME", Util.objectToString(m_entityUserManage.get("USER_NAME")));
        List<CDEEntity> listCheck = CDEService.LoadEntities(emUserManage, scCheck);

        if (listCheck.Count > 0)
        {
            showMessageErrAndOk("已存在此用户！");
            return;
        }

        // 如果一切正常，更新用户
        CDEService.UpdateEntities(emUserManage, listUserManage); 
        if (m_entityUserManage.Result == CDEDataOperationResult.ERROR)
        {
            
            //showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
            this.setControls(m_entityUserManage, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            // 跳回来源页 
            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert(\"");
            // 新增用户成功!
            if (m_entityUserManage.Operation == CDEDataOperation.INSERT)
            {
                sbr.AppendFormat("新增用户成功!");
            }
            else
            {
                sbr.AppendFormat(Resources.Resource.RESULT_OK);
            }
            sbr.AppendFormat("\");window.location='../admin/UserList.aspx?EM=" + EM_FULL_NAME + "&sel_menu_id=" + Sel_MenuNode.Attributes["id"].Value + "&IsBackSc=1';</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup2", sbr.ToString());
        }
    }
    /// <summary>
    /// 取消按钮事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        StringBuilder sbr2 = new StringBuilder();
        sbr2.AppendFormat("../admin/UserList.aspx?EM=" + EM_FULL_NAME + "&sel_menu_id=" + Sel_MenuNode.Attributes["id"].Value + "&IsBackSc=1");
        Response.Redirect(sbr2.ToString());
    }
}
